package com.infore.qualityControl.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_SMALL_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String DATE_SMALL_YMDH = "yyyy-MM-dd HH:00";
	public static final String DATE_SMALL_YMDHH = "yyyy-MM-dd HH";
	public static final String DATE_SMALL_YMDHMS = "yyyy-MM-dd HH:00:00";
	public static final String DATE_SMALL_STR = "yyyy-MM-dd";
	public static final String DATE_SMALL_YM = "yyyy-MM";
	public static final String DATE_ALL_STR = "yyyyMMddHHmmss";

	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	public static String getCurrentDate(String patten) {
		SimpleDateFormat format = new SimpleDateFormat(patten);
		return format.format(new Date());
	}

	/**
	 * 当月最后一天
	 * 
	 * @return
	 */
	public static String getLastDayM(Date d, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
		Date theDate = calendar.getTime();
		return df.format(theDate);
	}

	/**
	 * 当月最后一天(含小时),适用于年月
	 * 
	 * @return
	 */
	public static Date getLastDayMonthM(Date d, String pattern) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY),
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);
		Date theDate = calendar.getTime();
		return theDate;
	}

	/**
	 * 当月最后一天(含小时),适用于年月日
	 * 
	 * @return
	 */
	public static Date getLastDayDayM(Date d, String pattern) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(d);
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONDAY), calendar.get(Calendar.DAY_OF_MONTH),
				23, 59, 59);
		Date theDate = calendar.getTime();
		return theDate;
	}

	/**
	 * 
	 * 小时加减
	 */
	public static String addHour(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.HOUR, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	public static String addHour(String d, int n, String pattern) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.HOUR, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}
	
	public static Date addHour(Date d, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.HOUR, n);
		Date datNew = cal.getTime();
		return datNew;
	}
	/**
	 * 
	 * 日期加减
	 */
	public static String addDate(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.DATE, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	public static String addDate(String d, int n, String pattern) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.DATE, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	/**
	 * 
	 * 月份加减
	 */
	public static String addMonth(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.MONTH, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	public static String addMonth(String d, int n, String pattern) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.MONTH, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	/**
	 * 
	 * 年份加减
	 */
	public static String addYear(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	public static String addYear(String d, int n, String pattern) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sd.parse(d));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.add(Calendar.YEAR, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	/**
	 * 
	 * 分钟加减
	 */
	public static String addMin(Date d, int n, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.MINUTE, n);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	/**
	 * 
	 * 分钟加减
	 */
	public static Date addYear(Date d, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		cal.add(Calendar.YEAR, n);
		Date datNew = cal.getTime();
		return datNew;
	}

	/**
	 * 日期转换成字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	/**
	 * 日期转换成字符串 年 月 日
	 * 
	 * @param date
	 * @return
	 */
	public static String dateToStringYY(Date date) {
		SimpleDateFormat format = new SimpleDateFormat(DATE_SMALL_STR);
		String d = format.format(date);
		d = d.substring(0, 4) + "年" + d.substring(5, 7) + "月" + d.substring(8) + "日";
		return d;
	}

	/**
	 * 字符串转换日期
	 * 
	 * @param str
	 * @return
	 */
	public static Date stringToDate(String str, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		if (!str.equals("") && str != null) {
			try {
				return format.parse(str);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 查询当月天 列表 不包含今天
	 * 
	 * @return ["03-01","03-02"....]
	 */
	public static List<String> dangyueDayStringList() {
		List<String> list = new ArrayList<String>();
		Calendar ca = Calendar.getInstance();
		ca.setTime(new Date());
		int count = ca.get(Calendar.DAY_OF_MONTH);
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));

		SimpleDateFormat si = new SimpleDateFormat("MM-dd");
		String time = "";
		int temp = 1;
		while (temp < count) {
			time = si.format(ca.getTime());
			list.add(time);
			ca.add(Calendar.DAY_OF_MONTH, 1);

			temp++;
		}
		return list;
	}

	/**
	 * 日期第一天 20
	 * 
	 * @param date
	 * @return 2017-03-01
	 */
	public static String dangyueDiyitian(Date date, String pattern) {
		SimpleDateFormat si = new SimpleDateFormat(pattern);
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00);
		return si.format(ca.getTime());
	}

	/**
	 * 日期第一天(含小时),只适用于年月
	 * 
	 * @param date
	 * @return 2017-03-01
	 */
	public static Date dangyueDiyitianMonth(Date date, String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.getActualMinimum(Calendar.DAY_OF_MONTH), 00, 00, 00);
		return ca.getTime();
	}

	/**
	 * 日期第一天(含小时),只适用于年月日
	 * 
	 * @param date
	 * @return 2017-03-01
	 */
	public static Date dangyueDiyitianDay(Date date, String pattern) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(ca.get(Calendar.YEAR), ca.get(Calendar.MONDAY), ca.get(Calendar.DAY_OF_MONTH), 00, 00, 00);
		return ca.getTime();
	}

	/**
	 * *
	 * 
	 * @param args
	 */
	public static Date addDate2(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		Date datNew = cal.getTime();
		return datNew;
	}

	/**
	 * 获取年份
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取年份2
	 * 
	 * @param date
	 * @return
	 */
	public static int getYear(String date) {
		Date d = stringToDate(date, DATE_ALL_STR);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDAY_OF_YEAR(String date, String pattern) {
		Date d = stringToDate(date, pattern);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 获取月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.MONTH);
	}

	/**
	 * 获取月份
	 * 
	 * @param date
	 * @return
	 */
	public static int getDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取一年起始时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}

	public static String getFirstDay_Year(Date date, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int firstDay = cal.getActualMinimum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, firstDay);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	/**
	 * 获取一年结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFinalyDay_Year(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}

	public static String getFinalyDay_Year(Date date, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		Date datNew = cal.getTime();
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(datNew);
	}

	public static String getFirstDay_Month(String date, String pattern, String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int firstDat = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, firstDat);
		cal.set(Calendar.HOUR_OF_DAY, 00);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
		return new SimpleDateFormat(pattern2).format(cal.getTime());
	}

	public static String getFinalyDay_Month(String date, String pattern, String pattern2) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat(pattern).parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int finalDat = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		cal.set(Calendar.DAY_OF_MONTH, finalDat);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return new SimpleDateFormat(pattern2).format(cal.getTime());
	}

	/**
	 * 获取时间区间
	 * 
	 * @param d1 开始时间区间
	 * @param d2 结束时间区间
	 * @return 时间区间集合
	 */
	public static List<String> getTimeList(String d1, String d2, String pattern, String type) {
		SimpleDateFormat sd = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance();
		List<String> list = new ArrayList<String>();
		Date st, et;
		long sl, el, length;
		try {
			st = sd.parse(d1);
			et = sd.parse(d2);
			sl = st.getTime();
			el = et.getTime();
			if (type == "hour") {// 以小时为间隔
				length = (el - sl) / (60 * 60 * 1000);
			} else if (type == "day") {// 一天为间隔
				length = (el - sl) / (24 * 60 * 60 * 1000);
			} else {// 一月为间隔
				length = (el - sl) / (30L * 24L * 60L * 60L * 1000L); // 必须加L，否则溢出结果为负数
			}
			list.add(sd.format(st));
			for (int i = 0; i < length; i++) {
				c.setTime(st);
				if (type == "hour") {
					c.add(Calendar.HOUR, 1);
					st = c.getTime();
				} else if (type == "day") {
					c.add(Calendar.DAY_OF_YEAR, 1);
					st = c.getTime();
				} else {
					c.add(Calendar.MONTH, 1);
					st = c.getTime();
				}
				list.add(sd.format(st));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * @Title: getWeek @Description: 根据日期字符串判断是当月第几周 @param str @return @throws
	 * Exception 参数 @return int 返回类型 @throws
	 */
	public static int getMonthWeek(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		// 第几周
		int week = calendar.get(Calendar.WEEK_OF_MONTH);
		// 第几天，从周日开始
		// int day = calendar.get(Calendar.DAY_OF_WEEK);
		return week;
	}

	/**
	 * @Title: differentDays @Description: 计算两个时间相差的天数 @param date1 @param
	 * date2 @return 参数 @return int 返回类型 @throws
	 */
	public static int differentDays(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1 = cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if (year1 != year2) { // 同一年
			int timeDistance = 0;
			for (int i = year1; i < year2; i++) {
				if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年
					timeDistance += 366;
				} else {// 不是闰年
					timeDistance += 365;
				}
			}
			return timeDistance + (day2 - day1);
		} else {// 不同年
//			System.out.println("判断day2 - day1 : " + (day2 - day1));
			return day2 - day1;
		}
	}

	/**
	 * 计算2个日期之间相差的 相差多少年月日 比如：2011-02-02 到 2017-03-02 相差 6年，1个月，0天
	 * 
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	public static String dayComparePrecise(Date fromDate, Date toDate) {
		Calendar from = Calendar.getInstance();
		from.setTime(fromDate);
		Calendar to = Calendar.getInstance();
		to.setTime(toDate);

		int fromYear = from.get(Calendar.YEAR);
		int fromMonth = from.get(Calendar.MONTH);
		int fromDay = from.get(Calendar.DAY_OF_MONTH);

		int toYear = to.get(Calendar.YEAR);
		int toMonth = to.get(Calendar.MONTH);
		int toDay = to.get(Calendar.DAY_OF_MONTH);
		int year = toYear - fromYear;
		int month = toMonth - fromMonth;
		int day = toDay - fromDay;
		if (day < 0) {
			month -= 1;
			from.add(Calendar.MONTH, -1);
			day = day + from.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		if (month < 0) {
			month = (month + 12) % 12;
			year--;
		}
		return year + "年" + month + "月" + day + "日";
	}

	public static String hourComparePrecise(Date fromDate, Date toDate) {
		long diff = toDate.getTime() - fromDate.getTime(); // 这样得到的差值是微秒级别
		Calendar from = Calendar.getInstance();
		from.setTime(fromDate);
		Calendar to = Calendar.getInstance();
		to.setTime(toDate);
		int year = to.get(Calendar.YEAR) - from.get(Calendar.YEAR);// 获取年
		int month = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
		int day = to.get(Calendar.DAY_OF_MONTH) - from.get(Calendar.DAY_OF_MONTH);
		if (day < 0) {
			month -= 1;
			from.add(Calendar.MONTH, -1);
			day = day + from.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		if (month < 0) {
			month = (month + 12) % 12;
			year--;
		}
		long days = diff / (1000 * 60 * 60 * 24);
		long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); // 获取时 
		long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60); // 获取分钟
		long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60); // 获取秒
		return year + "年" + month + "月" + day + "日" + hours + "时" + minutes + "分" + s + "秒";
	}

	public static Date getTodayFirst() {
		
		Calendar calendar = Calendar.getInstance();
		// 时
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		// 分
		calendar.set(Calendar.MINUTE, 0);
		// 秒
		calendar.set(Calendar.SECOND, 0);
		// 毫秒
		calendar.set(Calendar.MILLISECOND, 0);

		Date time = calendar.getTime();
		return time;
	}
}
